package page;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

import ressource.Annonce;
import ressource.CadreAnnonce;
import ressource.CadreProduit;
import ressource.CadreVendeur;

/**
 * Servlet implementation class Accueil
 */
public class Accueil extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Accueil() {
        super();
    }
    
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Connection con = null;
		List<Annonce> annonces = new ArrayList<Annonce>();
		Annonce annonce;
		CadreProduit p;
		CadreVendeur v;
		CadreAnnonce a;
		
		try{
			Context namingContext = new InitialContext();
			DataSource dataSource = (DataSource)namingContext.lookup("java:comp/env/jdbc/MiniIBay");
			con = dataSource.getConnection();
			
			PreparedStatement pstmt=con.prepareStatement("SELECT `p`.id_produit, `a`.id_vendeur, `m`.note, `m`.nb_ventes, `m`.date_creation AS mdate_creation, `a`.commentaire, `a`.etat, `a`.id_annonce, `tp`.nom, `m`.pseudo, `a`.prix, `p`.intitule, `p`.description, `p`.photo, `a`.date_creation FROM `membre` m, `produit` p, `annonce` a, `type_produit` tp WHERE `p`.id_produit=`a`.id_produit AND `a`.id_vendeur=`m`.id_membre AND `p`.id_type=`tp`.id_type ORDER BY `a`.date_creation DESC LIMIT 0,5");
			ResultSet rs = pstmt.executeQuery();
			
			while(rs.next()){
				//On crée le produit :
				p = new CadreProduit();
				p.setId(rs.getInt("id_produit"));
				p.setType(rs.getString("nom"));
				p.setIntitule(rs.getString("intitule"));
				p.setDescription(rs.getString("description"));
				p.setPhoto(rs.getString("photo"));
				
				//On crée l'annonce :
				a = new CadreAnnonce();
				a.setIdAnnonce(rs.getInt("id_annonce"));
				a.setPrix(rs.getFloat("prix"));
				a.setEtat(rs.getString("etat"));
				a.setCommentaire(rs.getString("commentaire"));
		        a.setDateCreation(rs.getTimestamp("date_creation"));
				
				//On crée le vendeur :
				v = new CadreVendeur();
				v.setIdVendeur(rs.getInt("id_vendeur"));
				v.setPseudo(rs.getString("pseudo"));
				v.setNote(rs.getFloat("note"));
				v.setNbVentes(rs.getInt("nb_ventes"));
				v.setDateCreation(rs.getTimestamp("mdate_creation"));
				
				annonce = new Annonce();
				annonce.setAnnonce(a);
				annonce.setProduit(p);
				annonce.setVendeur(v);
				
				annonces.add(annonce);
			}
			
			request.setAttribute("annonces", annonces);
		}catch(Exception e){
			e.printStackTrace();
		} finally {
			try {
				if(con != null) con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		RequestDispatcher dis=request.getRequestDispatcher("home.jsp");
		dis.forward(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}
